import { NestFactory, Reflector } from '@nestjs/core'
import { AppModule } from './app.module'
import { ResponseInterceptor } from './common/response.interceptor'
import { HttpExceptionFilter } from './common/exception.filter'
import { ValidationPipe } from '@nestjs/common'
import { PORT, PREFIX } from 'config'
// import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  // 增加接口前缀
  process.env.NODE_ENV?.trim() === 'DEV' && app.setGlobalPrefix(PREFIX)
  // 对DTO 进行验证
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true, // 忽略 DTO 中未定义的属性
      forbidNonWhitelisted: true, // 禁止传入 DTO 中未定义的属性
      transform: true // 自动将请求数据转换为 DTO 类型
    })
  )
  const reflector = app.get(Reflector)

  // const options = new DocumentBuilder()
  //   .setTitle('Easy Admin API')
  //   .setVersion('1.0')
  //   .build()

  // const document = SwaggerModule.createDocument(app, options)
  // SwaggerModule.setup('api-docs', app, document)

  app.useGlobalInterceptors(new ResponseInterceptor(reflector))
  app.useGlobalFilters(new HttpExceptionFilter())

  await app.listen(process.env.PORT ?? PORT)
}
bootstrap()
